package com.atguigu.lease.web.app.custom.interceptor;

import com.atguigu.common.exception.LeaseException;
import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.common.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class AuthenticationInterceptor implements HandlerInterceptor {
//    这是一个拦截器，用来检查登录的人是否有合法的token

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//    前段发来请求request，我们从request中获取名为access_token的请求头信息，将其
//    存储到stirng类型的变量token中。
        String token = request.getHeader("access_token");
        if (token == null){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_AUTH);
        }
        JwtUtil.parseToken(token);
        return true;
    }
}
